package com.wt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wt.entity.Order;
import com.wt.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;


@WebServlet("/AddOrderServlet")
public class AddOrderServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        //时间处理
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        //使用工具解析json字符串
        JSONObject jsonObject = (JSONObject) JSON.parse(getJsonData(request));
        String orderName = jsonObject.getString("orderName");
        String orderPrice = jsonObject.getString("orderPrice");
        String scenicImage = jsonObject.getString("scenicImage");
        String userId = jsonObject.getString("userId");
        String reservationTime = sdf.format(c.getTime());
        //出发时间赋值，天数加3，时间置为9点
        c.add(Calendar.DAY_OF_MONTH,3);
        c.set(Calendar.HOUR_OF_DAY,9);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        String departTime = sdf.format(c.getTime());
        //生成Order对象并赋值
        Order order = new Order();
        order.setOrderName(orderName);
        order.setOrderPrice(Double.valueOf(orderPrice));
        order.setScenicImage(scenicImage);
        order.setUserId(userId);
        order.setOrderTime(reservationTime);
        order.setTravelTime(departTime);
        //添加数据到数据库
        OrderService orderService = new OrderService();
        PrintWriter out = response.getWriter();
        String data;
        try {
            if (orderService.addOrder(order)) {
                data = "{\"data\":true}";
            }else {
                data = "{\"data\":false}";
            }
            out.write(data);
            out.flush();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }
    //使用输入流将post请求的json数据格式化为字符串
    private String getJsonData(HttpServletRequest request) throws IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(request.getInputStream(),StandardCharsets.UTF_8);
        BufferedReader streamReader = new BufferedReader(inputStreamReader);
        StringBuilder sb = new StringBuilder();
        String inputStr;
        while ((inputStr = streamReader.readLine()) != null) {
            sb.append(inputStr);
        }
        return sb.toString();
    }
}
